package com.zablert.lazyutil.drawable;

import com.intellij.openapi.actionSystem.PlatformDataKeys;
import com.intellij.openapi.vfs.LocalFileSystem;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.vfs.VirtualFileManager;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiManager;
import com.zablert.lazyutil.utils.FileUtil;
import com.zablert.lazyutil.utils.Log;
import com.zablert.lazyutil.utils.VFileUtil;
import com.zablert.lazyutil.views.base.BasePage;
import com.zablert.lazyutil.views.buttongroup.IButtonGroupListener;
import com.zablert.lazyutil.views.buttongroup.ZButtonGroup;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.util.Arrays;

import static javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE;

public class ShapeGeneratePage extends BasePage {
    private final JFrame frameContainer;
    private JPanel panelMain;
    private JPanel panelTop;
    private JPanel panelCenter;
    private JPanel panelBottom;
    private JButton btnOk;
    private JButton btnCancel;
    private JRadioButton rbSolid;
    private JRadioButton rbHollow;
    private JRadioButton rbGradient;
    private JButton btnDirectionTitle;
    private String mPath;

    public void setPath(String path) {
        this.mPath = path;
    }

    public ShapeGeneratePage() {
        frameContainer = new JFrame("ShapeGenerateUtil");
        frameContainer.setContentPane(panelMain);
        frameContainer.setMinimumSize(new Dimension(600, 400));
        frameContainer.setLocationRelativeTo(null);
        frameContainer.getRootPane().setDefaultButton(btnOk);
        frameContainer.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        frameContainer.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                onCancel();
            }
        });

        btnOk.addActionListener(e -> onOK());

        btnCancel.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onCancel();
            }
        });

        btnDirectionTitle.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Log.d("mPath =" + mPath);
//                VirtualFile virtualFile = VirtualFileManager.getInstance().findFileByUrl(mPath + "/FUCK_TEST_PLUGIN_SHAPE.xml");
//                VFileUtil.createFile(mPath , "FUCK_TEST_PLUGIN_SHAPE.xml");
                FileUtil.writeToFile(ShapeUtil.INSTANCE.head()
                        .addCornerRadius(10)
                        .addSolid("#ffffff")
                        .addStroke(1, "#00ff00", 0, 0)
                        .addGradient("#10ff77ff", "#90808080")
                        .toShape(), mPath, "FUCK_TEST_PLUGIN_SHAPE3.xml");

//                VirtualFileManager.getInstance().asyncRefresh(new Runnable() {
//                    @Override
//                    public void run() {
//
//                    }
//                });
            }
        });

        // call onCancel() on ESCAPE
        panelMain.registerKeyboardAction(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onCancel();
            }
        }, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);

        initFillStyleButton();

    }

    private void initFillStyleButton() {
        ZButtonGroup btnGroup = new ZButtonGroup();
        btnGroup.add(rbSolid, rbHollow, rbGradient);
        btnGroup.addGroupListener(new IButtonGroupListener() {
            @Override
            public void itemChangeSelect(AbstractButton select) {


            }

            @Override
            public void itemChangeUnSelect(AbstractButton unselect) {
                Log.d("unselect =" + unselect.getText());
            }
        });
        btnGroup.setSelected(rbSolid, true);
    }

    private void onOK() {
        frameContainer.dispose();
    }

    private void onCancel() {
        frameContainer.dispose();
    }

    public void show() {
        frameContainer.setVisible(true);
    }


}
